import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import java.awt.FlowLayout;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.MatteBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JProgressBar;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JTextPane;


public class SvMonClient {

	private JFrame frmRmiSvmon;
	private JTextField textIp;
	private JTextField textPort;
	static public JButton btnPobierzDane;
	ISvMon mon;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SvMonClient window = new SvMonClient();
					window.frmRmiSvmon.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public SvMonClient() {
		initialize();
		Timer timer = new Timer( );
		Refresh zadanie = new Refresh( );
		timer.schedule(zadanie, 1000, 1000);
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		//Interfejs
		 
		//Defnicje przycisków
		final JMenuItem mntmPocz = new JMenuItem("Połącz");
		final JMenuItem mntmRozcz = new JMenuItem("Rozłącz");
		final JButton btnLog = new JButton("Loguj");
		final JButton btnDiscon = new JButton("Rozłącz");
		btnPobierzDane = new JButton("Pobierz Dane (temp)");
		btnPobierzDane.setVisible(false);
		final JLabel lblConErr = new JLabel("");
		lblConErr.setForeground(Color.RED);
		
		
		frmRmiSvmon = new JFrame();
		frmRmiSvmon.setTitle("RMI SvMon");
		frmRmiSvmon.setResizable(false);
		frmRmiSvmon.setBackground(new Color(102, 205, 170));
		frmRmiSvmon.setForeground(new Color(135, 206, 235));
		frmRmiSvmon.setBounds(100, 100, 730, 500);
		frmRmiSvmon.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frmRmiSvmon.setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("Program");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("Exit");
		mntmNewMenuItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				frmRmiSvmon.setVisible(false);
				frmRmiSvmon.dispatchEvent(new WindowEvent(frmRmiSvmon, WindowEvent.WINDOW_CLOSING));
				//frmRmiSvmon.dispose();
				System.exit(0);
			}
		});
		
		
		
		
		mnNewMenu.add(mntmPocz);
		
		
		
		mnNewMenu.add(mntmRozcz);
		mnNewMenu.add(mntmNewMenuItem);
		
		JScrollPane scrollPane = new JScrollPane();
		frmRmiSvmon.getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(32, 178, 170));
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignOnBaseline(true);
		flowLayout.setAlignment(FlowLayout.LEFT);
		flowLayout.setVgap(10);
		scrollPane.setColumnHeaderView(panel);
		
		JLabel lblNewLabel = new JLabel("IP: ");
		panel.add(lblNewLabel);
		
		textIp = new JTextField();
		textIp.setText("localhost");
		panel.add(textIp);
		textIp.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Port: ");
		panel.add(lblNewLabel_1);
		
		textPort = new JTextField();
		textPort.setText("1234");
		panel.add(textPort);
		textPort.setColumns(4);
		
		
		
		panel.add(btnLog);
		
		
		panel.add(btnDiscon);
		
		
		panel.add(btnPobierzDane);
		
		
		
		
		lblConErr.setFont(new Font("Verdana", Font.PLAIN, 14));
		panel.add(lblConErr);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(new Color(135, 206, 250));
		panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, Color.LIGHT_GRAY, Color.GRAY, Color.BLACK, Color.DARK_GRAY));
		scrollPane.setViewportView(panel_2);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(173, 216, 230));
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		
		JTextPane txtpnToDoList = new JTextPane();
		txtpnToDoList.setFont(new Font("Tahoma", Font.PLAIN, 10));
		txtpnToDoList.setText("TO DO LIST:\r\n\r\nRedefine domain port as you wish.\r\n\r\nDISCONECT BUTTON (Make it to disconnect previous connections, not only making viewed data cleared.\r\n\r\nLogin -> Info for exceptions behaviors (DONE)\r\n\r\nTimer - Get Data is Lame&Temporary\r\n\r\nTesting dear GLADoS, testing, oh look in front of me there is cool testing subject :)\r\n\r\n");
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 336, GroupLayout.PREFERRED_SIZE)
					.addGap(27)
					.addComponent(txtpnToDoList, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(174, Short.MAX_VALUE))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING)
						.addComponent(txtpnToDoList, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 239, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE))
					.addGap(152))
		);
		
		JLabel lblNewLabel_2 = new JLabel("CPU");
		lblNewLabel_2.setFont(new Font("Ravie", Font.BOLD | Font.ITALIC, 25));
		
		JLabel obtext = new JLabel("Śr. obciążenie (1 min)");
		
		final JLabel obwyp = new JLabel("");
		
		JLabel lblWersjaOS = new JLabel("Wersja OS: ");
		
		final JLabel lblProcOS = new JLabel("");
		
		JLabel lblNewLabel_4 = new JLabel("Model: ");
		
		final JLabel lblProcModel = new JLabel("");
		
		JLabel lblNewLabel_6 = new JLabel("Częstotliwość procesora:");
		
		final JLabel lblProcFreq = new JLabel("");
		
		JLabel lblNewLabel_8 = new JLabel("Liczba rdzeni: ");
		
		JLabel lblNewLabel_9 = new JLabel("");
		
		JLabel lblrObcienie = new JLabel("Śr. obciążenie (5 min)");
		
		final JLabel obwyp1 = new JLabel("");
		
		JLabel lblrObcienie_1 = new JLabel("Śr. obciążenie (15 min)");
		
		final JLabel obwyp3 = new JLabel("");
		
		final JProgressBar progressBar = new JProgressBar();
		
		final JProgressBar progressBar_1 = new JProgressBar();
		
		final JProgressBar progressBar_2 = new JProgressBar();
		
		final JLabel lblProcCoresNum = new JLabel("");
		
		final JLabel lblProcCoresOb = new JLabel("");
		
		JLabel lblKerV = new JLabel("");
		
		JLabel lblKernVer = new JLabel("");
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(lblWersjaOS)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblProcOS))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(lblNewLabel_8)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblProcCoresNum))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(lblNewLabel_9)

							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblProcCoresOb))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
									.addComponent(lblNewLabel_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(lblrObcienie, GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
									.addComponent(lblrObcienie_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(obtext, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(lblNewLabel_4)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(lblProcModel)))
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
										.addComponent(lblProcFreq)
										.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, false)
											.addComponent(obwyp1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
											.addComponent(obwyp, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(obwyp3, GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
											.addPreferredGap(ComponentPlacement.RELATED)))
									.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
										.addComponent(progressBar_2, Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
										.addComponent(progressBar_1, 0, 0, Short.MAX_VALUE)
										.addComponent(progressBar, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGap(66)
									.addComponent(lblKerV)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(lblKernVer)))))
					.addGap(74))
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(128)
					.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(135, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblWersjaOS)
						.addComponent(lblProcOS))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(lblProcModel)
						.addComponent(lblKerV)
						.addComponent(lblKernVer))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_6)
						.addComponent(lblProcFreq))
					.addGap(5)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_8)
						.addComponent(lblProcCoresNum))
					.addGap(5)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_9)
						.addComponent(lblProcCoresOb))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(obtext)
							.addComponent(obwyp)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(lblrObcienie)
						.addComponent(obwyp1)
						.addComponent(progressBar_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblrObcienie_1)
							.addComponent(obwyp3))
						.addComponent(progressBar_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(32, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		panel_2.setLayout(gl_panel_2);
		
		
		//Metody Akcji i Działań
		frmRmiSvmon.addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent arg0) {
				mntmPocz.setVisible(true);
				mntmRozcz.setVisible(false);
				btnDiscon.setEnabled(false);
				btnPobierzDane.setEnabled(false);
				btnLog.setEnabled(true);
				
				
			}
			@Override
			public void windowActivated(WindowEvent arg0) {
			}
		});
		
		mntmPocz.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				try {
					Object o = Naming.lookup("rmi://" + textIp.getText() + ":" + textPort.getText() + "/monitor");
		            mon = (ISvMon)o;
		            
		            mntmPocz.setVisible(false);
					mntmRozcz.setVisible(true);
					btnDiscon.setEnabled(true);
					btnPobierzDane.setEnabled(true);
					btnLog.setEnabled(false);
		        } catch (MalformedURLException f) {
		        	lblConErr.setText("MalFormedURL");
		        	
		            f.printStackTrace();
		        } catch (RemoteException f) {
		        	lblConErr.setText("RemoteException");
		            // TODO Auto-generated catch block
		            f.printStackTrace();
		        } catch (NotBoundException f) {
		        	lblConErr.setText("NotBound");
		            // TODO Auto-generated catch block
		            f.printStackTrace();
		        }
			}
		});
		
		mntmRozcz.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				mntmPocz.setVisible(true);
				mntmRozcz.setVisible(false);
				btnDiscon.setEnabled(false);
				btnPobierzDane.setEnabled(false);
				btnLog.setEnabled(true);
				
				btnDiscon.setEnabled(false);
				btnPobierzDane.setEnabled(false);
				btnLog.setEnabled(true);
				mntmPocz.setVisible(true);
				mntmRozcz.setVisible(false);
				
				lblProcOS.setText("");
				lblProcModel.setText("");
				lblProcFreq.setText("");
				lblProcCoresNum.setText("");
				lblProcCoresOb.setText("");
				obwyp.setText("");
				obwyp1.setText("");
				obwyp3.setText("");
				progressBar.setValue(0);
				progressBar_1.setValue(0);
				progressBar_2.setValue(0);
			}
		});
		
		btnDiscon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnDiscon.setEnabled(false);
				btnPobierzDane.setEnabled(false);
				btnLog.setEnabled(true);
				mntmPocz.setVisible(true);
				mntmRozcz.setVisible(false);
				
				lblProcOS.setText("");
				lblProcModel.setText("");
				lblProcFreq.setText("");
				lblProcCoresNum.setText("");
				lblProcCoresOb.setText("");
				obwyp.setText("");
				obwyp1.setText("");
				obwyp3.setText("");
				progressBar.setValue(0);
				progressBar_1.setValue(0);
				progressBar_2.setValue(0);
			}
		});
		btnPobierzDane.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int CoreFrqNum ;
					float CoreFrq[];
					float CoreFrqLoad[];
					String CoreFrqList = "";
				lblProcOS.setText(mon.osName());
				lblProcModel.setText(mon.cpuModel());
				
				if (mon.cpuFrequency() == 0 )
				{
					lblProcFreq.setText("0 MhZ");
				}
				else
				{
				lblProcFreq.setText(Float.toString(mon.cpuFrequency()/1000));
				}
				CoreFrqNum = mon.cpuNum();
				lblProcCoresNum.setText(Integer.toString(CoreFrqNum));
				CoreFrq = mon.cpuCoreFrequency();
				for (float i : CoreFrq)
				{
				   if (i == 0 )
				   {
					   CoreFrqList = CoreFrqList + "0" ;
				   }
				   else
				   {
					   CoreFrqList = CoreFrqList + Float.toString(i/1000) ;
				   }
				   
				   CoreFrqList = CoreFrqList + "MhZ / " ;
				}
				//lblProcCoresOb.setText(CoreFrqList);
				CoreFrqLoad = mon.cpuLoad();
				obwyp.setText(Float.toString(CoreFrqLoad[0]));
				obwyp1.setText(Float.toString(CoreFrqLoad[1]));
				obwyp3.setText(Float.toString(CoreFrqLoad[2]));
				
				if (CoreFrqLoad[0] > 100 ) {CoreFrqLoad[0] = 100;};
				if (CoreFrqLoad[1] > 100 ) {CoreFrqLoad[0] = 100;};
				if (CoreFrqLoad[2] > 100 ) {CoreFrqLoad[0] = 100;};
				
				progressBar.setValue((int)CoreFrqLoad[0]);
				progressBar_1.setValue((int)CoreFrqLoad[1]);
				progressBar_2.setValue((int)CoreFrqLoad[2]);
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnLog.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
		});
		btnLog.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
		            Object o = Naming.lookup("rmi://" + textIp.getText() + ":" + textPort.getText() + "/monitor");
		            mon = (ISvMon)o;
		            
		            btnDiscon.setEnabled(true);
					btnPobierzDane.setEnabled(true);
					btnLog.setEnabled(false);
					mntmPocz.setVisible(false);
					mntmRozcz.setVisible(true);
		            
					lblConErr.setText("");
		        } catch (MalformedURLException e) {
		        	lblConErr.setText("MalFormedURL");
		        	
		            e.printStackTrace();
		        } catch (RemoteException e) {
		        	lblConErr.setText("RemoteException");
		            // TODO Auto-generated catch block
		            e.printStackTrace();
		        } catch (NotBoundException e) {
		        	lblConErr.setText("NotBound");
		            // TODO Auto-generated catch block
		            e.printStackTrace();
		        }
				
				
			}
		});
	}
}

class Refresh extends TimerTask 
{ 

public void run( ) 
 { 
	SvMonClient.btnPobierzDane.doClick();
 } 
}
